package com.lsfb.light.Mine;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lsfb.light.Mine.About.AboutOurActivity;
import com.lsfb.light.Mine.MyCollection.MyCollectActivity;
import com.lsfb.light.Mine.MyInfo.MyInfoActivity;
import com.lsfb.light.Mine.MySubscription.MySubscriptionActivity;
import com.lsfb.light.R;
import com.lsfb.light.Utils.DataClearManager;
import com.lsfb.light.Utils.LsfbUtils;
import com.lsfb.light.Utils.ProgressDialogManager;
import com.zgscwjm.lsfbbasetemplate.LsfbFragment;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;

/**
 * Created by Administrator on 2018/4/23 0023.
 */

public class MineFragment extends LsfbFragment {

    private View view;

    @ViewInject(R.id.tv_mine_cache)
    private TextView tv_cache;

    private ProgressDialogManager dialogManager;

    private static final int CLEAN_SUC = 1001;
    private static final int CLEAN_FAIL = 1002;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLEAN_FAIL:
                    T.showShort(getActivity(), "清理失败");
                    break;
                case CLEAN_SUC:
                    Toast.makeText(getActivity(), "清理成功", Toast.LENGTH_LONG).show();
                    try {
                        String cacheSize = DataClearManager.getTotalCacheSize(getActivity());
                        tv_cache.setText(cacheSize);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg_mine, null);
        ViewInjectUtils.inject(this, view);
        dialogManager = new ProgressDialogManager(getActivity());

        try {
            String cacheSize = DataClearManager.getTotalCacheSize(getActivity());
            tv_cache.setText(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;

    }

    @Override
    protected void Resume() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_mine_info, R.id.ll_mine_collect, R.id.ll_mine_subscription,
            R.id.ll_mine_about, R.id.ll_mine_cache, R.id.ll_mine_exit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_mine_info:             //查看个人资料
                startActivity(new Intent(getActivity(), MyInfoActivity.class));
                break;
            case R.id.ll_mine_collect:          //我的收藏
                startActivity(new Intent(getActivity(), MyCollectActivity.class));
                break;
            case R.id.ll_mine_subscription:     //我的订阅
                startActivity(new Intent(getActivity(), MySubscriptionActivity.class));
                break;
            case R.id.ll_mine_about:            //关于我们
                startActivity(new Intent(getActivity(), AboutOurActivity.class));
                break;
            case R.id.ll_mine_cache:            //清除缓存
                clearCache();
                break;
            case R.id.ll_mine_exit:             //退出登录
                LsfbUtils.TokenError(getActivity());
                T.showShort(getActivity(), "退出账号成功");
                break;
        }
    }

    /**
     * 清除缓存
     */
    public void clearCache() {
        new Thread() {
            @Override
            public void run() {
                Message msg = new Message();
                try {
                    DataClearManager.clearAppCache(getActivity());
                    msg.what = CLEAN_SUC;
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.what = CLEAN_FAIL;
                }
                handler.sendMessage(msg);
            }
        }.start();
    }
}
